package com.voe;

import com.voe.common.OutputParam;
import com.voe.common.ReturnValue;

/**This sub-API supports the following functionalities:<br/>
*<br/>
*- RTP header modification (time stamp and sequence number fields).<br/>
*- Playout delay tuning to synchronize the voice with video.<br/>
*- Playout delay monitoring.<br/>
*<br/>
*Usage example, omitting error checking:<br/>
*<br/>
*VoiceEngine voe = VoiceEngine.createInterface();<br/>
*VoEBase base = VoEBase.createInterface(voe);<br/>
*VoEVideoSync vsync  = VoEVideoSync.createInterface(voe);<br/>
*base.init();<br/>
*...<br/>
*ReturnValue<Integer> output = new ReturnValue<Integer>();<br/>
*vsync.getPlayoutBufferSize(output);<br/>
*int buffer_ms = output.getValue();<br/>
*...<br/>
*base.trminate();<br/>
*base.release();<br/>
*vsync.release();<br/>
*voe.release();<br/>
*/
public class VoEVideoSync {

	// [start] apis
	
	public static VoEVideoSync createInstance(VoiceEngine voiceEngine) {

		long p = GetInterface(voiceEngine._p);
		if(p != 0)
			return new VoEVideoSync(p);
		return null;
	}

	public void release() {
		if (_p != 0) {
			Release(_p);
		}
	}

	/** Gets the current sound card buffer size (playout delay).
	 */
	public int getPlayoutBufferSize(ReturnValue<Integer> bufferMs) {
		OutputParam output = new OutputParam();
		int result = GetPlayoutBufferSize(_p, output);
		if(result == 0) {
			bufferMs.setValue((Integer)output.getObject()[0]);
		}
		return result;
	}

	/** Sets an additional delay for the playout jitter buffer.
	 */
	public int setMinimumPlayoutDelay(int channel, int delayMs) {
		return SetMinimumPlayoutDelay(_p, channel, delayMs);
	}

	/** Sets an initial delay for the playout jitter buffer. The playout of the<br/>
	 *  audio is delayed by |delay_ms| in millisecond.
	 */
	public int setInitialPlayoutDelay(int channel, int delay_ms) {
		return SetMinimumPlayoutDelay(_p, channel, delay_ms);
	}

	/** Gets the sum of the algorithmic delay, jitter buffer delay, and the<br/>
	 *  playout buffer delay for a specified |channel|.
	 */
	public int getDelayEstimate(int channel, ReturnValue<Integer> delayMs) {
		OutputParam output = new OutputParam();
		int result = GetPlayoutBufferSize(_p, output);
		if(result == 0) {
			delayMs.setValue((Integer)output.getObject()[0]);
		}
		return result;
	}

	/** Manual initialization of the RTP timestamp.
	 */
	public int setInitTimestamp(int channel, int timestamp) {
		return SetInitTimestamp(_p, channel, timestamp);
	}

	/** Manual initialization of the RTP sequence number.
	 */
	public int setInitSequenceNumber(int channel, short sequenceNumber) {
		return SetInitSequenceNumber(_p, channel, sequenceNumber);
	}

	/** Get the received RTP timestamp
	 */
	public int getPlayoutTimestamp(int channel, ReturnValue<Integer> timestamp) {
		OutputParam output = new OutputParam();
		int result = GetPlayoutBufferSize(_p, output);
		if(result == 0) {
			timestamp.setValue((Integer)output.getObject()[0]);
		}
		return result;
	}

	protected VoEVideoSync(long p) {
		_p = p;
	}

	protected long _p;
	
	// [end]
	
	
	
	
	
	
	
	
	

	// [start] jni api

	// Factory for the VoEVideoSync sub-API. Increases an internal
	// reference counter if successful. Returns NULL if the API is not
	// supported or if construction fails.
	private static native long GetInterface(long p_voiceEngine);

	// Releases the VoEVideoSync sub-API and decreases an internal
	// reference counter. Returns the new reference count. This value should
	// be zero for all sub-API:s before the VoiceEngine object can be safely
	// deleted.
	private static native int Release(long p_VoEVideoSync);

	// Gets the current sound card buffer size (playout delay).
	private static native int GetPlayoutBufferSize(long p_VoEVideoSync,
			OutputParam bufferMs);

	// Sets an additional delay for the playout jitter buffer.
	private static native int SetMinimumPlayoutDelay(long p_VoEVideoSync,
			int channel, int delayMs);

	// Sets an initial delay for the playout jitter buffer. The playout of the
	// audio is delayed by |delay_ms| in millisecond.
	private static native int SetInitialPlayoutDelay(long p_VoEVideoSync,
			int channel, int delay_ms);

	// Gets the sum of the algorithmic delay, jitter buffer delay, and the
	// playout buffer delay for a specified |channel|.
	private static native int GetDelayEstimate(long p_VoEVideoSync,
			int channel, OutputParam delayMs);

	// Manual initialization of the RTP timestamp.
	private static native int SetInitTimestamp(long p_VoEVideoSync,
			int channel, int timestamp);

	// Manual initialization of the RTP sequence number.
	private static native int SetInitSequenceNumber(long p_VoEVideoSync,
			int channel, short sequenceNumber);

	// Get the received RTP timestamp
	private static native int GetPlayoutTimestamp(long p_VoEVideoSync,
			int channel, OutputParam timestamp);

	// [end]
}
